package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.PageBean;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//常用注解：
//	类上:@Api(tags = "部门接口",description = "部门接口详细描述")
//	方法上:@ApiOperation(value = "部门查找",notes = "根据ID查找部门")
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增或修改
     * @param dept
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门新增或修改",notes = "新增或修改")
    public AjaxResult saveOrUpdate(@RequestBody Department dept){
        String operate  =null;
        try {
            if (dept.getId()==null) {
                operate = "新增";
                departmentService.save(dept);
            }
            else{
                operate = "修改";
            }
            String deptIdPath = "/"+dept.getId();
            if(dept.getParent()==null){
                dept.setDirPath(deptIdPath);
            }else {
                dept.setDirPath(dept.getDirPath()+deptIdPath);
            }
            departmentService.update(dept);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure(operate);
        }
    }

//    void remove(Long id);
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "根据id删除")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure("删除");
        }
    }
    @PatchMapping("/delete")
    @ApiOperation(value = "部门删除",notes = "批量删除")
    public AjaxResult patch(@RequestBody List<Long> ids){
        try {
            departmentService.patch(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createFailure("删除");
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "部门查找",notes = "根据id查找某个部门")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }
    @GetMapping
    @ApiOperation(value = "部门查找",notes = "查找所有部门")
    public List<Department> findAll(){
        return departmentService.findAll();
    }
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页查询")
    public PageBean<Department> queryData(@RequestBody DepartmentQuery query){
//        System.err.println(query.getName());
        return departmentService.queryData(query);
    }
    @PostMapping("/deptTree")
    @ApiOperation(value = "部门查找",notes = "部门次级查询")
    public List<Department> deptTree(){
        return departmentService.getDeptTree();
    }

//
//
//    Department findById(Long id );
//
//    /**
//     * 分页查询
//     *
//     */
//    //查询总条数
//    Integer queryCount(DepartmentQuery query);
//    //查询当前的页数
//    PageBean<Department> queryData(DepartmentQuery query);
}
